package com.example.smart.adapter.home

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.example.smart.R
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.data.AiConversationData
import com.example.smart.databinding.ItemChatReceiveBinding
import com.example.smart.databinding.ItemChatSendBinding

class ChatAdapter(val activity: Activity) : BaseRecyclerAdapter<ViewBinding, AiConversationData>(activity) {
    var type = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ViewBinding> {
        return if (viewType == 0) {
            BaseViewHolder(ItemChatSendBinding.bind(LayoutInflater.from(context)
                .inflate(R.layout.item_chat_send, parent, false)))
        } else {
            BaseViewHolder(ItemChatReceiveBinding.bind(LayoutInflater.from(context)
                .inflate(R.layout.item_chat_receive, parent, false)))
        }
    }

    override fun onBindView(itemView: ViewBinding, bean: AiConversationData, position: Int) {
        if (getItemViewType(position) == 0) {
            with(itemView as ItemChatSendBinding) {
                ItemMessage.text = bean.msg
            }
        } else {
            with(itemView as ItemChatReceiveBinding) {
                ItemMessage.text = bean.msg
                ItemImage.setImageResource(when (type) {
                    2 -> R.mipmap.icon_chat_photo5
                    3 -> R.mipmap.icon_chat_photo2
                    4 -> R.mipmap.icon_chat_photo3
                    5 -> R.mipmap.icon_chat_photo4
                    else -> R.mipmap.icon_chat_photo1
                })
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return getItemAtPosition(position).type
    }

}